#ifndef _DBPROXYSERVER_H_
#define _DBPROXYSERVER_H_

#include <windows.h>
#include <FileParser.h>
#include <SolarDatabaseDefine.h>
#include <SolarDatabase.h>
#include <QueryResult.h>
using namespace util;

class DBUser;
class IOCPServer;
class SolarDatabase;
class NetworkObject;
class DBSerialGenerator;

class DBProxyServer
{
	enum { IOHANDLER_SERVER_IDX = 0, };
	enum QUERY_INDEX 
	{ 
		QUERY_SELECTDBSERIAL = 0,
		QUERY_UPDATEDBSERIAL,
		QUERY_UPDATECHARACTER,
		QUERY_UPDATEWAREHOUSE,
	};

	friend void			CallBackCommand( char * szMessage );
public:
	DBProxyServer();
	~DBProxyServer();

	BOOL				Init();
	VOID				Release();
	VOID				Run();
	BOOL				Update();
	
	VOID				StartServerListen( char * pszIP, WORD wPort );
	DWORD				ConnectToServer( NetworkObject * pNetworkObject, char * pszIP, WORD wPort );
	VOID				DisplayServerInfo();

	CFileParser *		GetFileParser() { return m_pFileParser;	}
	eSERVER_TYPE		GetServerType() { return GAME_DBPROXY;	}

	VOID				ExecuteSelectSerialQuery();
	VOID				ExecutUpdateSerialQuery();
	VOID				ExecuteUpdateCharacterInfo( DBUser * pUser );
	VOID				ExecutUpdateWarehouseQuery( DBUser * pUser );
	DBSERIAL			AllocDBSerial();
	VOID				DBResult( DWORD dwIndex, Query * pData );
	VOID				UpdateFPS();
	DWORD				GetServerGUID() { return m_dwServerGUID; }
	VOID				SetServerGUID( DWORD guid ) { m_dwServerGUID=guid; }


	VOID				Shutdown();

private:
	enum				eSHUTDOWNSTATE { RUNNING, READYTOSHUTDOWN, };
	VOID				_setShutdownState( eSHUTDOWNSTATE state ) { m_eShutdownState = state;	}
	BOOL				ProcessConsole();
	eSHUTDOWNSTATE		m_eShutdownState;
	SolarDatabase		m_DB;
	IOCPServer *		m_pIOCPServer;
	DWORD				m_dwFPS;

	CFileParser *		m_pFileParser;
	DBSerialGenerator *	m_pDBSerialGenerator;
	DWORD				m_dwServerGUID;
};

extern DBProxyServer g_DBProxyServer;

#endif _DBPROXYSERVER_H_